﻿using UnityEngine;
using System.Collections;

public class PauseMenuScript : MonoBehaviour {

	bool inPauseMenu = false;
	GameObject quitMenu;

	float menuMin = 9.60141f;
	float menuMax = -0.7429f;

	Animator checkYesAnim;
	SpriteRenderer checkYesRend;
	Animator checkNoAnim;
	SpriteRenderer checkNoRend;

	// Use this for initialization
	void Start () {
		this.quitMenu = (GameObject)GameObject.Find("QuitMenu");

		this.checkYesAnim = (Animator)GameObject.Find ("CheckYes").GetComponent (typeof(Animator));
		this.checkYesRend = (SpriteRenderer)GameObject.Find ("CheckYes").GetComponent (typeof(SpriteRenderer));
		this.checkNoAnim = (Animator)GameObject.Find ("CheckNo").GetComponent (typeof(Animator));
		this.checkNoRend = (SpriteRenderer)GameObject.Find ("CheckNo").GetComponent (typeof(SpriteRenderer));

		this.checkNoAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.CheckNoUp);
		this.checkYesAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.CheckYesUp);

	}
	
	// Update is called once per frame
	void Update () {

		bool inCheckYes = false; bool inCheckYesInitially = false;
		bool inCheckNo = false; bool inCheckNoInitially = false;

		if (Input.GetKeyUp(KeyCode.Escape)) {
			this.inPauseMenu = !this.inPauseMenu;
			Vector3 pos = this.quitMenu.transform.position;
			if (this.inPauseMenu) {
				pos.y = menuMax;
				Time.timeScale = 0f;
			}
			else {
				pos.y = menuMin;
				Time.timeScale = 1f;
			}
			this.quitMenu.transform.position = pos;
		}

		if (this.inPauseMenu) {
			if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {
				inCheckNo = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkNoRend, GameMaintainerSingleton.Instance.Pos);
				inCheckYes = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkYesRend, GameMaintainerSingleton.Instance.Pos);
				inCheckNoInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkNoRend, GameMaintainerSingleton.Instance.ClickDownPos);
				inCheckYesInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.checkYesRend, GameMaintainerSingleton.Instance.ClickDownPos);


			}
		}

		// Yes Button
		if (GameMaintainerSingleton.Instance.CheckButton(
			inCheckYes,inCheckYesInitially,
			checkYesRend,checkYesAnim,
			MenuButtonConstants.MenuConstants.CheckYesDown,MenuButtonConstants.MenuConstants.CheckYesUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.LoadLevel(0);
		}
		// No Button
		else if (GameMaintainerSingleton.Instance.CheckButton(
			inCheckNo,inCheckNoInitially,
			checkNoRend,checkNoAnim,
			MenuButtonConstants.MenuConstants.CheckNoDown,MenuButtonConstants.MenuConstants.CheckNoUp,
			MenuButtonConstants.MenuTriggerString)) {
			this.inPauseMenu = false;
			Vector3 pos = this.quitMenu.transform.position;
			if (this.inPauseMenu) {
				pos.y = menuMax;
				Time.timeScale = 0f;
			}
			else {
				pos.y = menuMin;
				Time.timeScale = 1f;
			}
			this.quitMenu.transform.position = pos;
		}

	}
}
